﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess
{
    public class ClubDataAccess : OrganizationalDivisionsDataAccess
    {
        public ClubDataAccess(string dbName, string conn, bool usePredefinedMetadata)
            :base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<Club> SelectClubsList()
        {
            try
            {
                //Select OrganizationalDivision List
                List<OrganizationalDivision> orgDivList = SelectOrganizationalDivisionsList();

                //Select Clubs List
                List<CLUB> clubList = dataContext.CLUB.ToList();

                //Merge result
                var res = (from orgDiv in orgDivList
                           join club in clubList on orgDiv.ID equals club.ID
                           select new Club
                          {
                              DataBaseName = dataBaseName,
                              CLASS_ID = orgDiv.CLASS_ID,
                              EXT_ID = orgDiv.EXT_ID,
                              ID = orgDiv.ID,
                              NAME = orgDiv.NAME,
                              PARENT_ID = orgDiv.PARENT_ID,
                              BOSS_ID = orgDiv.BOSS_ID,
                              DATE_CREATED = orgDiv.DATE_CREATED,
                              DESCRIPTION = orgDiv.DESCRIPTION,
                              EMAIL = orgDiv.EMAIL,
                              SLOGAN = orgDiv.SLOGAN,
                              ROOM_ID = club.ROOM_ID,
                              SPECIALITY = club.SPECIALITY
                          }).ToList();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectClubBy_Id");
            }
        }

        public Club SelectClubBy_Id(int Id)
        {
            try
            {
                //Select OrgDiv part by Id
                OrganizationalDivision orgDivPart = SelectOrganizationalDivisionBy_Id(Id);

                //Select Club part
                List<CLUB>  clubPart = dataContext.CLUB.ToList();

                //Merge result
                var res = (from club in clubPart
                           where club.ID == orgDivPart.ID
                           select new Club
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = orgDivPart.CLASS_ID,
                               EXT_ID = orgDivPart.EXT_ID,
                               ID = orgDivPart.ID,
                               NAME = orgDivPart.NAME,
                               PARENT_ID = orgDivPart.PARENT_ID,
                               BOSS_ID = orgDivPart.BOSS_ID,
                               DATE_CREATED = orgDivPart.DATE_CREATED,
                               DESCRIPTION = orgDivPart.DESCRIPTION,
                               EMAIL = orgDivPart.EMAIL,
                               SLOGAN = orgDivPart.SLOGAN,
                               ROOM_ID = club.ROOM_ID,
                               SPECIALITY = club.SPECIALITY
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectClubBy_Id");
            }
        }

        public void InsertClub(Club club, ref int generated_Id)
        {
            try
            {
                //Insert the OrgDivision part
                InsertOrganizationalDivision(club, ref generated_Id);

                //Insert the Club part
                int new_Id = generated_Id;
                CLUB clubPart = new CLUB();
                clubPart.ORGANIZATIONAL_DIVISION = dataContext.ORGANIZATIONAL_DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                clubPart.ID = new_Id;
                clubPart.ROOM_ID = club.ROOM_ID;
                clubPart.SPECIALITY = club.SPECIALITY;
               
                //Submit changes
                dataContext.AddToCLUB(clubPart);
                dataContext.SaveChanges();
            }
            catch(Exception ex)
            {
                throw new Exception("Error while InsertClub");
            }
        }

        public void UpdateClub(Club club)
        {
            try
            {
                //Update the OrganizationalDivision part
                UpdateOrganizationalDivision(club);

                //Update the Club part
                CLUB clubPart = dataContext.CLUB.Where(t => t.ID == club.ID).FirstOrDefault();
                clubPart.ROOM_ID = club.ROOM_ID;
                clubPart.SPECIALITY = club.SPECIALITY;
                               
                //Submit changes
                dataContext.SaveChanges();
            }
            catch(Exception ex)
            {
                throw new Exception("Error while UpdateClub");
            }
        }

        public void DeleteClub(Club club)
        {
            try
            {
                //Delete the Club part
                CLUB divPart = dataContext.CLUB.Where(t => t.ID == club.ID).FirstOrDefault();

                dataContext.DeleteObject(divPart);
                dataContext.SaveChanges();

                //Delete the OrganizationalDivision part
                DeleteOrganizationalDivision(club);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteClub");
            }
        }
    }
}
